package com.happyrecyclingstation.controller.jobseeker;

import com.happyrecyclingstation.dto.UserDto;
import com.happyrecyclingstation.entity.User;
import com.happyrecyclingstation.service.UserService;
import com.happyrecyclingstation.utils.CurrentHolder;
import com.happyrecyclingstation.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 求职者
 */
@Slf4j
@RequestMapping("/jobSeeker/users")
@RestController("jobSeekerUserController")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 获取个人信息
     * @return
     */
    @GetMapping
    public Result getInfo(){
        log.info("求职者获取个人信息");
        User user = userService.getUserById(CurrentHolder.getCurrentId());
        return Result.success(user);
    }

    /**
     * 更新个人信息
     * @param userDto
     * @return
     */
    @PutMapping
    public Result updateInfo(@RequestBody UserDto userDto){
        log.info("求职者更新个人信息:{}", userDto);
        userService.update(userDto);
        return Result.success();
    }

    /**
     * 更新密码
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @PutMapping("/pwd")
    public Result updataPwd(String oldPwd, String newPwd){
        log.info("求职者更新密码:{},{}", oldPwd, newPwd);
        Boolean result = userService.updatePwd(oldPwd, newPwd);
        return result ? Result.success() : Result.error("旧密码错误");
    }
}
